import apis from "../services/planApi";

export default {
  namespace: "planlist",
  state: {
    planList: [],
    totalCount: 0,
    loading: false,
    onLineErr: -1,
    offLineErr: -1,
    artificialCode: -1,
    departList: []
  },
  effects: {
    *planList({ payload }, { call, put }) {
      yield put({
        type: "put/putloading",
        payload: true
      });
      const res = yield call(apis.planList, payload);
      const { datas, totalCount } = res;
      yield put({
        type: "get/getplanList",
        payload: datas
      });
      yield put({
        type: "put/putloading",
        payload: false
      });
      yield put({
        type: "put/puttotal",
        payload: totalCount
      });
    },
    *putOnline({ payload }, { call, put }) {
      const res = yield call(apis.putOnline, payload);
      const { errCode } = res;
      yield put({
        type: "get/onLineErr",
        payload: errCode
      });
    },
    *putOffLine({ payload }, { call, put }) {
      const res = yield call(apis.putOffLine, payload);
      const { errCode } = res;
      yield put({
        type: "get/offLineErr",
        payload: errCode
      });
    },
    *artificialPush({ payload }, { call, put }) {
      const res = yield call(apis.artificialPush, payload);
      const { errCode } = res;
      yield put({
        type: "get/artificialCode",
        payload: errCode
      });
    },
    *getDepart({ payload }, { call, put }) {
      const res = yield call(apis.getDepart, payload);
      const { datas } = res;
      yield put({
        type: "get/departList",
        payload: datas
      });
    },
    *delPlan({ payload }, { call }) {
      const res = yield call(apis.delPlan, payload);
      return res;
    }
  },
  reducers: {
    "get/getplanList": (state, { payload }) => ({
      ...state,
      planList: payload
    }),
    "put/putloading": (state, { payload }) => ({
      ...state,
      loading: payload
    }),
    "put/puttotal": (state, { payload }) => ({
      ...state,
      totalCount: payload
    }),
    "get/onLineErr": (state, { payload }) => ({
      ...state,
      onLineErr: payload
    }),
    "get/offLineErr": (state, { payload }) => ({
      ...state,
      offLineErr: payload
    }),
    "get/artificialCode": (state, { payload }) => ({
      ...state,
      artificialCode: payload
    }),
    "get/departList": (state, { payload }) => ({
      ...state,
      departList: payload
    })
  }
};
